// 用户信息仓库文件  登录界面处用到
import { defineStore } from "pinia";
import { ref } from "vue";
import { login_api ,accInfo_api} from "../api/apis.ts";//接口
import local from "../utils/local.ts"//本地
import { useRouter } from "vue-router";//路由


export let useUser = defineStore("useUser", () => {
    let $router = useRouter()
    // 初始化就取本地数据
    let $role = ref(local.get("role"));
    let $token = ref(local.get("tk"));

     // 仓库数据 ！断言  三目
     let $accInfo=ref( local.get("info")? JSON.parse(local.get("info")!): "")

    //  登录处的
    let $loginapi = async (data: Login) => {
        let res = await login_api(data);
        let { code, role, token } = res.data;
        if (code == 0) {
            $role.value = role;
            $token.value = token;
            local.set("role", role);
            local.set("tk", token);
            // 页面跳转
            $router.push("/home/index")
        }
    }
   
    // 个人中心 右侧顶部导航 修改头像
    let $getInfo_store =async ()=>{    
        let res=await accInfo_api()
        console.log(res);
        $accInfo.value=res.data;
        // 存本地
        local.set("info",JSON.stringify(res.data))
    }

    return{
        $role,
        $token,
        $loginapi,
        $accInfo,
        $getInfo_store 
    }
})


